﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AG.Data;
using AG.MapEditor.Data;

namespace AGMapEditor.Controls
{
    public partial class AGMapObjectPanel : UserControl, IMapPanelObserver
    {
        private AGEMapPanel _ctlMapPanel;
        public AGMapObjectPanel(AGEMapPanel mapPanel)
        {
            InitializeComponent();

            _ctlMapPanel = mapPanel;
            _ctlMapPanel.Attach(this);
            BindObjectTree();
        }

        private void BindObjectTree()
        {
            _ctlTreeObject.Nodes.Clear();

            if (_ctlMapPanel.Map != null)
            {
                for (int objIndex = 0; objIndex < AGMEContext.Current.Data.ObjectCategoryList.Count; objIndex++)
                {
                    AGMapObjectCategory objCategory = AGMEContext.Current.Data.ObjectCategoryList[objIndex];
                    TreeNode objCategoryNode = new TreeNode();
                    objCategoryNode.Text = objCategory.ToString();
                    objCategoryNode.Tag = objCategory;
                    _ctlTreeObject.Nodes.Add(objCategoryNode);

                    for (int index = 0; index < _ctlMapPanel.Map.ObjectList.Count; index++)
                    {
                        AGMapObject mapObject = _ctlMapPanel.Map.ObjectList[index];

                        if (mapObject.CategoryId == objCategory.Id)
                        {
                            TreeNode node = new TreeNode();
                            node.Text = mapObject.ToString();
                            node.Tag = mapObject;
                            objCategoryNode.Nodes.Add(node);
                        }
                    }
                }
            }
        }

        public void UpdateDesignState()
        {
        }


        public void UpdateObjectList()
        {
            BindObjectTree();
        }

        public void UpdateSwitchPointList()
        {
        }

        private void _ctlTreeObject_AfterSelect(object sender, TreeViewEventArgs e)
        {
            AGMapObject mapObject = e.Node.Tag as AGMapObject;
            if (mapObject != null)
            {
                _ctlMapPanel.SelectObject(mapObject);
            }
        }
    }
}
